package gui;

import java.io.File;
import java.io.Serializable;

import javax.swing.table.AbstractTableModel;

public class ExtractBlackTableModel extends AbstractTableModel implements Serializable{
	private static final long serialVersionUID = 42558903487883051L;
	private String[] columnNames={"Image","Rotation","Lower Left, X",
			"Lower Left, Y", "Upper Right, X","Upper Right, Y", "X Grid Size",
			"Y Grid Size","Threshold"
	};
	private Object[][] data;
	private Class types[]={String.class,Double.class,Integer.class,Integer.class,
			Integer.class, Integer.class,  Integer.class, Integer.class,
			Double.class
	};
	
	public ExtractBlackTableModel()
	{
	}
	
	
	public int getColumnCount() {
		return columnNames.length;
	}
	
	public String getColumnName(int col)
	{
		return columnNames[col];
	}
	
	public Class getColumnClass(int col)
	{
		return types[col];
	}

	public int getRowCount() {
		return data==null?0:data.length;
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		if(data==null)
		{
			return null;
		}
		else if(rowIndex <data.length && columnIndex<data[rowIndex].length)
		{
			return data[rowIndex][columnIndex];
		}
		throw(new IllegalArgumentException("Cell out of bounds: ("+rowIndex+","+
				columnIndex));
	}
	
	public void setValueAt(Object value, int row, int col)
	{
		data[row][col]=value;
		fireTableCellUpdated(row,col);
	}
	
	public boolean isCellEditable(int row,int col)
	{
		if(data!=null)
			return true; //that is, all cells can be edited.
		else
			return false;
	}
	
	public void addRow()
	{
		Object row[]={"",0.0,0,0,0,0,0,0,0.0};
		addRow(row);
	}
	
	public void addRow(Object [] row)
	{
		if(data==null)
		{
			data=new Object[1][];
			data[0]=row;
		}
		else
		{
			Object [][] tempData=new Object[data.length+1][];
			System.arraycopy(data, 0, tempData, 0, data.length);
			tempData[data.length]=row;
			data=tempData;
		}
		fireTableDataChanged();
	}
	
	public void removeRow(int row)
	{
		if(data.length==1)
		{
			data=null;
		}
		else
		{
			Object [][] tempData=new Object [data.length-1][];
			//copy the elements before the deleted
			System.arraycopy(data, 0, tempData, 0, row);
			//copy the elements after the deleted
			System.arraycopy(data, row+1, tempData, row, tempData.length-row);
			data=tempData;
		}
		fireTableDataChanged();
	}
}
